package org.bees.cms.core.io;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.bees.cms.common.Files;
import org.bees.cms.common.Tree;

public class Directory extends IO implements Tree<Directory> {
	private static final long serialVersionUID = 503742360912784764L;

	public static Directory root;
	static {
		try {
			File file = new File(Files.absolute(""));
			root = new Directory();
			root.from(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	{
		flag = false;
	}

	private Directory parent;
	private Set<Directory> children = new HashSet<Directory>();
	private Set<Document> documents = new HashSet<Document>();

	public Directory() {
		super();
	}

	public Directory(String path) throws Exception {
		super(path);
	}

	@Override
	public long size() throws Exception {
		long size = 0;
		{
			Iterator<Directory> iterator = children.iterator();
			while (iterator.hasNext()) {
				size += iterator.next().size();
			}
		}
		{
			Iterator<Document> iterator = documents.iterator();
			while (iterator.hasNext()) {
				size += iterator.next().size();
			}
		}
		return size;
	}

	@Override
	public void save() throws Exception {
		File temp = new File(Files.absolute(parent.getPath() + "/" + name));
		temp.mkdir();
	}

	// 必须先把目录中所有的子目录 和 文件删除 才能删除该目录 那么这就是一个后序遍历
	@Override
	public void omit() throws Exception {
		File[] files = file.listFiles();
		for (int i = 0; files != null && i < files.length; i++) {
			File file = files[i];
			if (file.isDirectory()) {
				Directory directory = new Directory();
				directory.from(file);
				directory.omit();
			}
			if (file.isFile()) {
				Document document = new Document();
				document.from(file);
				document.omit();
			}
		}
		file.delete();
	}

	@Override
	public void name(String name) throws Exception {
		File temp = new File(Files.absolute(parent.getPath() + "/" + name));
		file.renameTo(temp);
		this.from(temp);
	}

	@Override
	public void move(String path) throws Exception {
		File temp = new File(Files.absolute(path + "/" + name));
		temp.mkdir();
		File[] files = file.listFiles();
		for (int i = 0; files != null && i < files.length; i++) {
			File file = files[i];
			if (file.isDirectory()) {
				Directory directory = new Directory();
				directory.from(file);
				directory.move(path + "/" + name);
			}
			if (file.isFile()) {
				Document document = new Document();
				document.from(file);
				document.move(path + "/" + name);
			}
		}
		omit();
	}

	@Override
	public void copy(String path) throws Exception {
		File temp = new File(Files.absolute(path + "/" + name));
		temp.mkdir();
		File[] files = file.listFiles();
		for (int i = 0; files != null && i < files.length; i++) {
			File file = files[i];
			if (file.isDirectory()) {
				Directory directory = new Directory();
				directory.from(file);
				directory.copy(path + "/" + name);
			}
			if (file.isFile()) {
				Document document = new Document();
				document.from(file);
				document.copy(path + "/" + name);
			}
		}
	}

	@Override
	public void hide() throws Exception {
		File[] files = file.listFiles();
		for (int i = 0; files != null && i < files.length; i++) {
			File file = files[i];
			if (file.isDirectory()) {
				Directory directory = new Directory();
				directory.from(file);
				directory.hide();
			}
			if (file.isFile()) {
				Document document = new Document();
				document.from(file);
				document.hide();
			}
		}
		Runtime.getRuntime().exec("attrib " + file.getAbsolutePath() + " +H");
	}

	@Override
	public void show() throws Exception {
		File[] files = file.listFiles();
		for (int i = 0; files != null && i < files.length; i++) {
			File file = files[i];
			if (file.isDirectory()) {
				Directory directory = new Directory();
				directory.from(file);
				directory.show();
			}
			if (file.isFile()) {
				Document document = new Document();
				document.from(file);
				document.show();
			}
		}
		Runtime.getRuntime().exec("attrib " + file.getAbsolutePath() + " -H");
	}

	public boolean empty() throws Exception {

		return false;
	}

	@Override
	public void from(File t) throws Exception {
		super.from(t);
		if (!this.equals(root)) {
			parent = new Directory();
			parent.from(t.getParentFile());
		}
	}

	@Override
	public File to() throws Exception {
		return super.to();
	}

	@Override
	public List<Directory> ancestors() {
		List<Directory> ancestors = new ArrayList<Directory>();
		Directory temp = this;
		while (temp != null) {
			ancestors.add(temp);
			temp = temp.parent;
		}
		return ancestors;
	}

	@Override
	public List<Directory> descendants() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Directory getParent() {
		return parent;
	}

	@Override
	public void setParent(Directory parent) {
		this.parent = parent;
	}

	@Override
	public Set<Directory> getChildren() {
		return children;
	}

	@Override
	public void setChildren(Set<Directory> children) {
		this.children = children;
	}

	public Set<Document> getDocuments() {
		return documents;
	}

	public void setDocuments(Set<Document> documents) {
		this.documents = documents;
	}

}
